﻿var alreadyOpenTableId = null;
var alreadyOpenSubTableId = null;
var hrefBaseValue = null;
var funId = "";
var submitFlg = false;
var checkedFlg = false;
var parCheckedFlg = false;
var oldFunctionId = null;
var editFlg = false;
var basename="/base";
var SubImg = basename+'/gximages/style1/menu/menu_arrow_close.gif';
var SubImgOpen = basename+'/gximages/style1/menu/menu_arrow_open.gif';
function closeMenu() {
	if (alreadyOpenTableId == null) return;
	alreadyOpenTable = document.all(alreadyOpenTableId);
	targetTableId = alreadyOpenTableId + "d";
	targetTable = document.all(targetTableId);
	targetTable.style.display = "none";
	alreadyOpenTableId = null;
}

function closeSubMenu(){
	if(alreadyOpenSubTableId == null) return;
	tableSubSrcImgId = alreadyOpenSubTableId + "_img";
	tableSubSrcImg = document.all(tableSubSrcImgId);
	alreadyOpenSubTable = document.getElementById(alreadyOpenSubTableId);
	targetSubTableId = alreadyOpenSubTableId + "d";

	targetSubTable = document.getElementById(targetSubTableId);
	if(targetSubTable!=null){
		targetSubTable.style.display = "none";
		tableSubSrcImg.src = SubImg;
	}
	alreadyOpenSubTableId = null;	
}

function menuClick(tableSrc) {
	var currentTableSrc = tableSrc;
	if (currentTableSrc.id != alreadyOpenTableId) closeMenu();
    	if(countRecord(tableSrc) > 0){
    		targetTableId = currentTableSrc.id+"d";
    		targetTable = document.all(targetTableId);
	    	if (targetTable.style.display == "none") {
	        	targetTable.style.display = '';
			alreadyOpenTableId = currentTableSrc.id;
	    	} else {
	        	targetTable.style.display = "none";
		}
	}
}

function subMenuClick(tableSrc) {
	var currentSubTableSrc = tableSrc;
	if (currentSubTableSrc.id != alreadyOpenSubTableId) closeSubMenu();
	var index = (tableSrc.id).indexOf('_');
	var lastIndex = (tableSrc.id).lastIndexOf('_');
	var tableId = tableSrc.id;
	var funcId = (index == lastIndex) ? tableId.substring(index+1):tableId.substring(index+1,lastIndex);
    	subTableId = "MEMU_"+funcId + "d";
    	subTable = document.all(subTableId);
	var tableSrcImgId = (index == lastIndex) ? tableSrc.id+"_img":tableSrc.id;
	var tableSrcImg = document.all(tableSrcImgId);
	if (subTable != null) {
	
	    if (subTable.style.display == "none") {
	        subTable.style.display = '';
	        alreadyOpenSubTableId = tableId;
	        tableSrcImg.src = SubImgOpen;
	    } else {
	        subTable.style.display = "none";
	        tableSrcImg.src = SubImg;
	   }
	}
	
}


function st_onload() {
	return;
}

function clickMenuCheck(obj){
	var tableId = obj.id;
	var funcId = tableId.substring(tableId.indexOf('_')+1);
	var checkAry = document.getElementsByName(funcId);
	
	if( checkAry != null && checkAry.length > 0){	
		if(confirmAllMenu()){
			var parId = document.getElementById("MEMUCHECK_"+obj.name);
			if(parId != null){
				var parCheckedAry = document.getElementsByName(obj.name);
				for(var m = 0;m < parCheckedAry.length;m++){
					if(parCheckedAry[m].checked){
						checkedFlg = true;
						break;
					}
				}
				parId.checked = checkedFlg;
				checkedFlg = false;
			}
			for(var i = 0;i < checkAry.length;i++){
				checkAry[i].checked = document.getElementById(tableId).checked;
				var subCheckAry = document.getElementsByName(checkAry[i].id.substring(checkAry[i].id.indexOf('_')+1));
				if(subCheckAry != null){
					for(var j = 0;j<subCheckAry.length;j++){
						subCheckAry[j].checked = document.getElementById(tableId).checked;
					}	
				}
			}
		}else{
			obj.checked = obj.checked ? false:true;
		}
	}else{
		if(obj.name != ""){
			var brotherAry = document.getElementsByName(obj.name);
			if(obj.id.substring(obj.id.indexOf('_')+1) == parent.listPage.parentFuncId){
				var listPageCheck = parent.listPage.document.getElementsByName("checkBox");
				if(listPageCheck != null || listPageCheck.length > 0){
					for(var i = 0;i<listPageCheck.length;i++){
						listPageCheck[i].checked = obj.checked;
					}	
				}
			}	
			if(brotherAry != null){
				for(var n = 0;n < brotherAry.length;n++){
					if(brotherAry[n].checked) {
						checkedFlg = true;
						break;	
					}
				}
			}
			document.getElementById("MEMUCHECK_"+obj.name).checked = checkedFlg;
			var parparId = document.getElementById("MEMUCHECK_"+obj.name).name;
			if(document.getElementById("MEMUCHECK_"+parparId)!=null){
				var parparObj = document.getElementsByName(parparId);
				for(var j = 0;j<parparObj.length;j++){
					if(parparObj[j].checked){
						parCheckedFlg = true;
						break;
					}
				}
				document.getElementById("MEMUCHECK_"+parparId).checked = parCheckedFlg;
			} 
			checkedFlg = false;
			parCheckedFlg = false;
		}
	}
}


//判断是否存在下级对象
function countRecord(obj){
	var cId = obj.id;
	var cFuncId = cId.substring(cId.indexOf('_')+1);
	var checkAry = document.getElementsByName(cFuncId);
	return checkAry.length;	
}

//取消重复元素
function getUnique(str){
	var arry = str.split(";");
	var tempAry = arry.slice(0);
	for(var i = 0;i<tempAry.length;i++){
		for(var j = i + 1;j<tempAry.length;){
			if(tempAry[j] == tempAry[i]){
				tempAry.splice(j,1);	
			}else{
				j++;
			}
		}
	}
	return tempAry.join(";");	
}


//编辑按钮权限
function onClickEdit(obj){
	var functionId = obj.id;
	alert("oldFunctionId:"+oldFunctionId);
	if(oldFunctionId != null){
		var oldSpanObj = document.getElementById(oldFunctionId);
		oldSpanObj.style.color = "#000000"; 	
	}
	document.getElementById(functionId).style.color = "red";
	oldFunctionId = functionId;
	alert("functionId:"+functionId);
	parent.listPage.document.getElementById("parentId").value = functionId.substring(functionId.indexOf('_')+1);
	parent.listPage.document.getElementById("funcs").value = document.getElementById('CHECK_'+functionId.substring(functionId.indexOf('_')+1)).value;
	if(parent.listPage.checkFlg){
		var listPageCheckAry = parent.listPage.document.getElementsByName("checkBox");
		var str = "";
		for(var i = 0;i < listPageCheckAry.length; i++ ){
			if(listPageCheckAry[i].checked){
				str += listPageCheckAry[i].id + "#";
			}
		}
		if(parent.listPage.parentFuncId != ""){
			var oldParentId = parent.listPage.parentFuncId;
			alert("CHECK_FUNC"+oldParentId);
			document.getElementById("CHECK_FUNC"+oldParentId).value = str + oldParentId + "#";
			//document.getElementById("CHECK_"+oldParentId).checked = str != "" ? true:false ;
			clickMenuCheck(document.getElementById("CHECK_FUNC"+oldParentId));
		}
		parent.listPage.checkFlg = false;
	}
	parent.listPage.document.actForm.submit();	
}

//保存权限
function saveBtn(){
	var str = "";
	var idList = ";";
	var inputList = self.frames["treePage"].document.actForm.getElementsByTagName("input");	
	var listCheckAry = listPage.document.getElementsByName("checkBox");
	if(listCheckAry != null){
		var listFunctionId = "";
		for(var j = 0;j<listCheckAry.length;j++){
			if(listCheckAry[j].checked){
				listFunctionId += listCheckAry[j].id + ";";	
			}
		}
		if(treePage.document.getElementById("CHECK_"+listPage.parentFuncId) != null){
			if(!treePage.document.getElementById("CHECK_"+listPage.parentFuncId).checked){
				treePage.document.getElementById("CHECK_"+listPage.parentFuncId).checked = listFunctionId != "" ? true:false;
			}
			treePage.document.getElementById("CHECK_"+listPage.parentFuncId).value = listFunctionId + listPage.parentFuncId + ";";
		}
	}
	
	for(var i = 0;i < inputList.length; i++){
		if(inputList[i].type == "checkbox" && inputList[i].checked){
			var funcid = inputList[i].id;
			var index = funcid.indexOf('_');
			if(funcid.substring(0,index) == "CHECK" && inputList[i].value != null){
				str += inputList[i].value + ";";
			}
		}	
	}
	var ary = str.split(";");
	for (var i = 0; i < ary.length; i++) {
		if (ary[i] != "" && ary[i] != null ) {
			idList += ary[i] + ";";
		}
	}
	document.all.actForm.functionName.value = idList;
	if (!submitFlg) {
		document.all.actForm.submit();
		submitFlg = true;
	}
}
